#include "dominion.h"
#include "dominion_helpers.h"
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <math.h>
#include <time.h>
#include <stdlib.h>

int main(){
  struct gameState state;
  struct gameState preState;

  int n;
  int p;
  int i;
  int err;
  int card;
  int fail;
  int treas_amt_discard;
  int treas_amt_deck;

  int drawntreasure;
  int cardDrawn;
  int temphand[MAX_HAND];
  int z;
  int flag;


srand(time(NULL));


      for (n = 0; n < 20000; n++) {
        for (i = 0; i < sizeof(struct gameState); i++) {
          ((char*)&state)[i] = rand() % 256;
        }
        p = rand() % 2;

        //TODO set player
        state.numPlayers = 2;
        state.deckCount[p] = rand() % MAX_DECK;
        for(i=0; i< state.deckCount[p]; i++){
          state.deck[p][i] = rand() % 25;
        }
        state.discardCount[p] = rand() % MAX_DECK;
        for(i=0; i< state.discardCount[p]; i++){
          state.discard[p][i] = rand() % 25;
        }
        state.handCount[p] = rand() % MAX_HAND;
        for(i=0; i< state.handCount[p]; i++){
          state.hand[p][i] = rand() % 25;
        }
        state.whoseTurn = p;

        memcpy (&preState, &state, sizeof(struct gameState));

        //Count number of treasure in player deck and discard
        for(i=0; i< preState.deckCount[p]; i++){
          card = preState.deck[p][i];
          if(card == copper || card == silver || card == gold){
            treas_amt_deck++;
          }
        }

        for(i=0; i< preState.discardCount[p]; i++){
          card = preState.discard[p][i];
          if(card == copper || card == silver || card == gold){
            treas_amt_discard++;
          }
        }

        //Choice 1, 2 and 3 don't matter as zzzadventurecardplay doesn' take them as values.
        zzzadventurercardplay(drawntreasure, p, cardDrawn, temphand, z, &state);


        if(treas_amt_deck+treas_amt_discard ==0){
            //If no treausure in deck hand should not increase
            if(state.handCount[p] != preState.handCount[p]){
              printf("TEST FAILURE. No treasure in discard/deck but hand increased.\n");
              fail++;
            }
        }else if(treas_amt_deck+treas_amt_discard >=1){
          if(state.hand[p][state.handCount[p]-1] != copper && state.hand[p][state.handCount[p]-1] != silver && state.hand[p][state.handCount[p]-1] != gold){
            printf("TEST FAILURE, One treasure found but not added to hand\n");
            fail++;
          }
        }else if(treas_amt_deck+treas_amt_discard >=2){
          if(state.hand[p][state.handCount[p]-1] != copper && state.hand[p][state.handCount[p]-1] != silver && state.hand[p][state.handCount[p]-1] != gold){
            flag++;
          }

          if(state.hand[p][state.handCount[p]-2] != copper && state.hand[p][state.handCount[p]-2] != silver && state.hand[p][state.handCount[p]-2] != gold){
            flag++;
          }
          if(flag){
          printf("TEST FAILURE, Two treasure found but not added to hand\n");
          fail++;
        }
      }

        //reset variables
        treas_amt_deck = 0;
        treas_amt_discard =0;
        int drawntreasure = 0;
        int cardDrawn = 0;
        int z =0;
        flag = 0;

      }

  printf("randomtestadventurer Summary: %i tests failed. %i percent failure rate", fail, (fail/2000) *100);
  return(0);
}
